/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pruebas;

import java.awt.*;
import java.awt.event.*;

/**
 *
 * @author OROZCO7
 */
public class PintarFiguras {

    int caso = 0;
    int x;
    int xx;
    int y;
    int yy;
    Frame f;
    MenuBar barm = new MenuBar();
    Menu me = new Menu("opciones");
    MenuItem rect = new MenuItem("rectangulo");
    MenuItem oval = new MenuItem("ovalo");
    MenuItem tri = new MenuItem("triangulo");
    MenuItem poli = new MenuItem("polinomio");
    todo todo;

    class todo extends Canvas {

        int ban = 1;

        @Override
        public void paint(Graphics g) {


            switch (caso) {


                case 1:

                    g.setColor(Color.red);
                    g.drawRect(xx, yy, x, y);
                    break;
                case 2:

                    g.setColor(Color.red);
                    g.drawOval(x, y, xx, yy);
                    break;
                case 3:
                    int Array[] = {x, yy, x*2};
                    int array[] = {y, xx, y*2};


                    g.drawPolygon(Array, array, 3);
                    break;

                case 4:

                    int Arreglo[] = {xx, x, xx, x*2, x*2, yy};
                    int arreglo[] = {yy, y, yy*2, y, y*2, xx};


                    g.drawPolygon(Arreglo, arreglo, 6);
                    break;

            }

        }

        void cachar(int x, int y) {

            if (ban == 1) {
                System.out.println("-click 1--" + x + " --- " + y);
                ban = 2;
            } else if (ban == 2) {

                xx = x;
                yy = y;
                ban = 1;
                System.out.println("--click 2----  " + xx + " --- " + yy);
                todo.repaint();
            }


        }
    }

    public void ventana() {
        todo = new todo();
        f = new Frame();

        f.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        rect.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                caso = 1;


            }
        });
        oval.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                caso = 2;


            }
        });
        tri.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                caso = 3;


            }
        });
        poli.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                caso = 4;


            }
        });

        todo.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                x = e.getX();
                y = e.getY();

                todo.cachar(x, y);
            }
        });
        me.add(poli);
        me.add(tri);
        me.add(oval);
        me.add(rect);
        barm.add(me);
        f.setMenuBar(barm);


        f.add(todo);

        f.setLocation(200, 100);
        f.setSize(500, 500);
        f.setVisible(true);

    }

    public static void main(String[] args) {
        PintarFiguras p = new PintarFiguras();
        p.ventana();

    }
}




